package org.kazuhiko.pokemon3d.utility.tools;

import java.util.ArrayList;

import org.lwjgl.opengl.Display;
import org.newdawn.slick.Color;

public class Debugger
{
	private static final Debugger instance = new Debugger();
	
	private ArrayList<DebugText> texts;
	
	public static Debugger getInstance()
	{
		return instance;
	}
	
	private Debugger()
	{
		texts = new ArrayList<DebugText>();
	}
	
	public void addDebugText( String text, Color color )
	{
		texts.add( new DebugText( text, color ) );
	}
	
	public void draw()
	{
		int textsPerRow = 0;
		for( int i = 10; i < Display.getHeight() - 30; i+=20 )
		{
			textsPerRow++;
		}
		
		int[] longestStrings = new int[ texts.size() / textsPerRow ];
		int longestString = 0;
		int currentTextNr = 1;
		int currentRow = 0;
		for( DebugText text : texts )
		{
			if( text.getText().length() > longestString )
			{
				longestString = text.getText().length();
			}
			
			if( currentTextNr == textsPerRow )
			{
				longestStrings[ currentRow ] = longestString;
				longestString = 0;
				currentRow++;
				currentTextNr = 0;
			}
			
			currentTextNr++;
		}
		
		float y = 10;
		float x = 10;
		currentRow = 0;
		currentTextNr = 1;
		for( DebugText text : texts )
		{
			TextRenderer.getRenderer().render2DText( text.getText(), text.getColor(), x, y );
			if( currentTextNr == textsPerRow )
			{
				y = 10;
				x += longestStrings[currentRow]*6 + 5;
				currentRow++;
				currentTextNr = 0;
			}
			else
			{
				y+= 20;
			}
			currentTextNr++;
		}
		texts.clear();
	}
	
	public class DebugText
	{
		private String text;
		private Color color;
		
		public DebugText( String text, Color color )
		{
			this.text = text;
			this.color = color;
		}
		
		public String getText()
		{
			return text;
		}
		
		public Color getColor()
		{
			return color;
		}
	}
}
